package tt.dz.order.pojo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tt.dz.order.dao.BaseDAOUtil;
import tt.dz.order.exception.BusinessException;

/**
 * @Title: 运营商提供的服务
 * @author zhoutian   
 * @date 2015-11-26
 */
public class ProviderService {
	//服务编号（联合主键）
	private String serviceNo;
	//@transient 服务名称
	private String serviceName;
	//提供该服务的运营商编码（联合主键）
	private String serviceProviderCode;
	//@transient 运营商名称
	private String merchantName;
	//计量单位编号
	private int unitNo;
	//@transient 计量单位名称
	private String unitName;
	//单价
	private BigDecimal unitPrice;
	//该运营商提供的服务是否是成本类的服务，0：否 1：是
	private int isCost;
	//服务状态，0：未提供（已撤销）1：正常提供
	private int status;
	//提供服务时间
	private Date startTime;
	//撤销服务时间
	private Date endTime;
	//创建人
	private String creator;
	//创建时间
	private Date createTime;
	
	public String getServiceNo() {
		return serviceNo;
	}

	public void setServiceNo(String serviceNo) {
		this.serviceNo = serviceNo;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getUnitName() {
		return unitName;
	}

	public void setUnitName(String unitName) {
		this.unitName = unitName;
	}

	public String getServiceProviderCode() {
		return serviceProviderCode;
	}

	public void setServiceProviderCode(String serviceProviderCode) {
		this.serviceProviderCode = serviceProviderCode;
	}

	public String getMerchantName() {
		return merchantName;
	}

	public void setMerchantName(String merchantName) {
		this.merchantName = merchantName;
	}

	public int getUnitNo() {
		return unitNo;
	}

	public void setUnitNo(int unitNo) {
		this.unitNo = unitNo;
	}

	public BigDecimal getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(BigDecimal unitPrice) {
		this.unitPrice = unitPrice;
	}

	public int getIsCost() {
		return isCost;
	}

	public void setIsCost(int isCost) {
		this.isCost = isCost;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	
	public ProviderService() {}
	
	private static final String select_all_sql = "select serviceNo, serviceProviderCode, unitNo, unitPrice, isCost, status, startTime, endTime, creator, createTime from order_service_provider";
	
	private static final String update_all_sql = "update order_service_provider set serviceNo = :serviceNo, serviceProviderCode = :serviceProviderCode, unitNo = :unitNo, unitPrice = :unitPrice, isCost = :isCost, status = :status, startTime = :startTime, endTime = :endTime, creator = :creator, createTime = :createTime";
	
	public void save() throws BusinessException{
		if(isServiceNoAndServiceProviderCodeExist(this.serviceNo, this.serviceProviderCode)){
			throw new BusinessException("该运营商提供服务已经存在");
		}
		String sql = "insert into order_service_provider(serviceNo, serviceProviderCode, unitNo, unitPrice, isCost, status, startTime, endTime, creator, createTime) values(:serviceNo, :serviceProviderCode, :unitNo, :unitPrice, :isCost, :status, :startTime, :endTime, :creator, :createTime)";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	public void update(){
		String sql = update_all_sql + " where serviceNo = :serviceNo and serviceProviderCode = :serviceProviderCode";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	public void delete(){
		String sql = "delete from order_service_provider where serviceNo = :serviceNo and serviceProviderCode = :serviceProviderCode";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("serviceNo", this.serviceNo);
		paramMap.put("serviceProviderCode", this.serviceProviderCode);
		BaseDAOUtil.delete(sql, paramMap);
	}
	
	public static boolean isServiceNoAndServiceProviderCodeExist(String serviceNo, String serviceProviderCode){
		ProviderService providerService = getProviderServiceBy(serviceNo, serviceProviderCode);
		if(providerService != null){
			return true;
		}
		return false;
	}
	
	/**
	 * 通过服务编号、服务提供商编号查询服务信息
	 * @param serviceNo 服务编号
	 * @param serviceProviderCode 服务提供商编号
	 * @return 服务信息
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static ProviderService getProviderServiceBy(String serviceNo, String serviceProviderCode){
		String sql = select_all_sql + " where serviceNo = :serviceNo and serviceProviderCode = :serviceProviderCode order by serviceNo asc";
		Map paramMap = new HashMap();
		paramMap.put("serviceNo", serviceNo);
		paramMap.put("serviceProviderCode", serviceProviderCode);
		return BaseDAOUtil.queryForObject(sql, paramMap, ProviderService.class);
	}
	
	/**
	 * 通过服务编号、服务提供商编号查询服务单价
	 * @param serviceNo 服务编号
	 * @param serviceProviderCode 服务提供商编号
	 * @return 单价
	 */
	public static BigDecimal originalUnitPriceQuery(String serviceNo, String serviceProviderCode){
		ProviderService pservice = getProviderServiceBy(serviceNo, serviceProviderCode);
		return pservice.getUnitPrice();
	}
	
	/**
	 * 查询运营商提供服务列表
	 * @return 运营商提供服务列表
	 */
	public static List<ProviderService> getAll(){
		String sql = "select ps.serviceNo, osi.name as serviceName, ps.serviceProviderCode, m.name as merchantName, ps.unitNo, u.name as unitName, ps.unitPrice, ps.isCost, ps.status, ps.startTime, ps.endTime, ps.creator, ps.createTime "
				+ " from order_service_provider ps"
				+ " inner join order_service_info osi on ps.serviceNo = osi.serviceNo "
				+ " inner join ms_merchant m on ps.serviceProviderCode = m.merchant_code and m.is_valid = 1"
				+ " inner join order_unit u on ps.unitNo = u.unitNo "
				+ " order by ps.serviceNo asc";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		return BaseDAOUtil.queryForList(sql, paramMap, ProviderService.class);
	}
}
